package com.zjdiepu.www.service.partner.impl;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.diepu.www.client.partner.proxy.request.ApplyDiviBackProxyRequest;
import com.diepu.www.client.partner.proxy.request.ApplyDiviStatusProxyRequest;
import com.diepu.www.client.partner.proxy.request.OpenCardbackReasonRequest;
import com.diepu.www.client.partner.proxy.request.UpdateDownloadStatusRequest;
import com.diepu.www.client.partner.proxy.request.UpdatePushStatusRequest;
import com.diepu.www.client.partner.proxy.response.SynInterviewInfoResponse;
import com.diepu.www.client.proxy.partner.common.DiepuOtherAttachmentsResponse;
import com.diepu.www.client.proxy.partner.request.own.DiepuOtherAttachmentsRequest;
import com.diepu.www.client.proxy.partner.request.own.DiepuYwOpenCardHzicbcRequest;
import com.zjdiepu.www.config.ClientConfig;
import com.zjdiepu.www.consts.PartnerService;
import com.zjdiepu.www.net.HttpClient;
import com.zjdiepu.www.request.BaseRequest;
import com.zjdiepu.www.response.BooleanResponse;
import com.zjdiepu.www.security.base.enums.ResultCode;
import com.zjdiepu.www.security.client.encode.BaseEncryptService;
import com.zjdiepu.www.security.exception.BasePartnerException;
import com.zjdiepu.www.security.server.core.Partners;
import com.zjdiepu.www.service.partner.IDiepuService;

/**
 * 碟普接口服务
 * @author xiongxiaotun
 *
 */
@Service
public class DiepuServiceImpl extends BaseEncryptService implements IDiepuService {
	private Partners partner = Partners.DIE_PU;
	/**
	 * 同步面签信息
	 * @param request
	 * @return
	 * @throws BasePartnerException 
	 */
	@Override
	public SynInterviewInfoResponse synInterviewInfo(BaseRequest request) throws BasePartnerException {
		//签名
		String signEncrypt = signEncrypt(request, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviBackJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.INTERVIEW_SYN_INTERVIEW_INFO, signEncrypt);
		SynInterviewInfoResponse synInterviewInfoResponse = valdateDencrypt(applyDiviBackJson, SynInterviewInfoResponse.class, partner);
		return synInterviewInfoResponse;
	}
	
	/**
	 * 同步面签视频:推送状态
	 * @param request
	 * @return
	 * @throws BasePartnerException 
	 */
	@Override
	public boolean pushStatus(UpdatePushStatusRequest request) throws BasePartnerException {
		//签名
		String signEncrypt = signEncrypt(request, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviBackJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.INTERVIEW_PUSH_STATUS, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviBackJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}
	
	/**
	 * 同步面签视频:下载状态
	 * @param request
	 * @return
	 * @throws BasePartnerException 
	 */
	@Override
	public boolean downloadStatus(UpdateDownloadStatusRequest request) throws BasePartnerException {
		//签名
		String signEncrypt = signEncrypt(request, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviBackJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.INTERVIEW_DOWNLOAD_STATUS, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviBackJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}
	

	/**
	 * 申请分期接口
	 * @param request
	 * @return
	 * @throws BasePartnerException 
	 */
	@Override
	public boolean applyDiviBack(ApplyDiviBackProxyRequest request) throws BasePartnerException {
		//签名
		String signEncrypt = signEncrypt(request, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviBackJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.APPLY_DIVI_BACK, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviBackJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}

	@Override
	public boolean applyDiviStatus(ApplyDiviStatusProxyRequest request)
			throws BasePartnerException {
		//签名
		String signEncrypt = signEncrypt(request, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviStatusJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.APPLY_DIVI_STATUS, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviStatusJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}

	@Override
	public boolean noticeOpenCardSuccess(DiepuYwOpenCardHzicbcRequest diepuYwOpenCardHzicbcRequest) throws BasePartnerException {
		
		//绛惧悕
		String signEncrypt = signEncrypt(diepuYwOpenCardHzicbcRequest, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviStatusJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.OPEN_CARD_SUCCESS, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviStatusJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}

	@Override
	public boolean openCardBackReason(OpenCardbackReasonRequest backNoticeRequest) throws BasePartnerException {
		
		String signEncrypt = signEncrypt(backNoticeRequest, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviStatusJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.OPEN_CARD_BACK_REASON, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviStatusJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
	}
	
	@Override
	public DiepuOtherAttachmentsResponse selectOtherAttachmentsByDataId(DiepuOtherAttachmentsRequest diepuOtherAttachmentsRequest) throws BasePartnerException{
		
		String signEncrypt = signEncrypt(diepuOtherAttachmentsRequest, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviStatusJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.OTHER_ATTACHMENTS, signEncrypt);
		DiepuOtherAttachmentsResponse response = valdateDencrypt(applyDiviStatusJson, DiepuOtherAttachmentsResponse.class, partner);
		return response;
	}

	/* (non-Javadoc)
	 * 更新开卡卡部状态
	 * @see com.zjdiepu.www.service.partner.IDiepuService#updateOpenCardStatus(com.diepu.www.client.proxy.partner.request.own.DiepuYwOpenCardHzicbcRequest)
	 */
	@Override
	public boolean updateOpenCardStatus(
			DiepuYwOpenCardHzicbcRequest diepuYwOpenCardHzicbcRequest) throws BasePartnerException {
		String signEncrypt = signEncrypt(diepuYwOpenCardHzicbcRequest, partner);
		if(StringUtils.isBlank(signEncrypt)) {
			throw new BasePartnerException(ResultCode.FAIL_PARAM_EMPTY);
		}
		String applyDiviStatusJson = HttpClient.postJson(ClientConfig.getPartnerGateWay(partner) + PartnerService.UPDATE_OPENCARD_STATUS, signEncrypt);
		BooleanResponse booleanResponse = valdateDencrypt(applyDiviStatusJson, BooleanResponse.class, partner);
		return booleanResponse.getResult();
		
	}
}
